package com.yh.autobio.modules.adatper;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yh.autobio.AppContext;
import com.yh.autobio.R;
import com.yh.autobio.modules.bean.User;
import com.yh.autobio.modules.bean.t_my_pgdanEntry01;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * Created by john on 2016/7/14.
 */
public class WorkDetailAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<t_my_pgdanEntry01> workInfos;
    private OnItemDeleteListener ItemDeleteListener;
    private DecimalFormat formartDouble;
    private int all_count;
    private double allPrive = 0;

    public int getAll_count() {
        return all_count;
    }

    public void setAll_count(int all_count) {
        this.all_count = all_count;
    }

    public ArrayList<t_my_pgdanEntry01> getWorkInfos() {
        return workInfos;
    }

    public WorkDetailAdapter(Context context, ArrayList<t_my_pgdanEntry01> workInfos, int all_count) {
        if (formartDouble == null) {
            formartDouble = new DecimalFormat("#.00");
        }
        this.all_count = all_count;
        mContext = context;
        this.workInfos = workInfos;
        User user = AppContext.getInstance().getCurrentUser();
        if (user != null) {
            allPrive = user.getFgongjia();
        }
    }

    private String getAverage(int count) {
        double i = (double) count / (double) workInfos.size();
        String strAverage = formartDouble.format(i);
        return strAverage;
    }

    public Double div(Double v1, Double v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @Override
    public int getCount() {
        return workInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return workInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_workentry_detail, null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }

        holder.tv_name.setText(workInfos.get(position).getFHBUserName());
        holder.et_price.setText(allPrive + "");

        //holder.et_money.setText(allPrive);
        holder.img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                workInfos.remove(position);
                if (ItemDeleteListener != null) {
                    ItemDeleteListener.OnItemDeleteListener(position);
                }
                notifyDataSetChanged();
            }
        });
        holder.et_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String strPrice = holder.et_price.getText().toString().trim();
                if (!".".equals(s.toString())) {
                    if (!TextUtils.isEmpty(s.toString())) {
                        if (!TextUtils.isEmpty(strPrice)) {
                            double price = Double.parseDouble(strPrice);
                            double num = Double.parseDouble(s.toString());
                            double count = price * num;
                            String format = formartDouble.format(count);
                            holder.et_money.setText(format);
                        }
                    } else {
                        holder.et_money.setText("");
                    }
                } else {
                    holder.et_money.setText("");
                }

            }
        });
        holder.et_num.setText(div((double) all_count, (double) workInfos.size(), 2) + "");

//        holder.et_money.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                workInfos.get(position).setFPrice(Double.parseDouble(holder.et_price.getText().toString().trim()));
//                workInfos.get(position).setFMoney(Double.parseDouble(holder.et_money.getText().toString().trim()));
//                workInfos.get(position).setFAmount(Double.parseDouble(holder.et_num.getText().toString().trim()));
//            }
//        });
        return convertView;
    }


    public void setOnItemDeleteListener(OnItemDeleteListener ItemDeleteListener) {
        this.ItemDeleteListener = ItemDeleteListener;
    }

    public interface OnItemDeleteListener {
        public void OnItemDeleteListener(int position);
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView img_delete;
        TextView tv_name;
        EditText et_num;
        TextView et_price;
        TextView et_money;

        public MyViewHolder(View view) {
            super(view);
            img_delete = (ImageView) view.findViewById(R.id.img_delete);
            et_num = (EditText) view.findViewById(R.id.et_num);
            et_price = (TextView) view.findViewById(R.id.et_price);
            et_money = (TextView) view.findViewById(R.id.et_money);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
        }
    }
}
